package drds.global_transaction.resource_manager.execute.dml;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import drds.global_transaction.resource_manager.ParameterObjectList;
import drds.global_transaction.resource_manager.execute.StatementExecuteTemplate;
import drds.global_transaction.resource_manager.model.Image;
import drds.global_transaction.resource_manager.model.TableMetaData;
import drds.global_transaction.resource_manager.proxy.resource_manager.StatementProxy;
import drds.global_transaction.resource_manager.sql_recognizer.DeleteSqlRecognizer;
import drds.global_transaction.resource_manager.sql_recognizer.SqlRecognizer;

public class DeleteExecutor<T, S extends Statement> extends AbstractDmlExecutor<T, S>
{

	@SuppressWarnings("unchecked")
	public DeleteExecutor(SqlRecognizer sqlRecognizer, StatementProxy statementProxy, StatementExecuteTemplate statementExecuteTemplate)
	{
		super(sqlRecognizer, statementProxy, statementExecuteTemplate);
	}

	@Override
	protected Image beforeImage() throws SQLException
	{
		DeleteSqlRecognizer deleteSqlRecognizer = (DeleteSqlRecognizer) sqlRecognizer;
		TableMetaData tableMetaData = getTableMetaData(deleteSqlRecognizer.getTableName());
		List<String> columnNameList = new ArrayList<>();
		for (String columnName : tableMetaData.getColumnNameToColumnMetaDataMap().keySet())
		{
			columnNameList.add(columnName);
		}

		StringBuilder sb = new StringBuilder("select ");
		int size = columnNameList.size();
		for (int i = 0; i < size; i++)
		{
			sb.append(columnNameList.get(i));
			if (i < (size - 1))
			{
				sb.append(", ");
			}
		}
		String whereCondition = null;
		ArrayList<Object> objectList = new ArrayList<>();
		if (statementProxy instanceof ParameterObjectList)
		{
			whereCondition = deleteSqlRecognizer.getWhereCondition((ParameterObjectList) statementProxy, objectList);
		} else
		{
			whereCondition = deleteSqlRecognizer.getWhereCondition();
		}
		sb.append(" from " + tableMetaData.getTableName() + " where " + whereCondition + " for update");
		String sql = sb.toString();

		Image beforeImage = null;
		//
		Statement statement = null;
		//
		ResultSet resultSet = null;
		try
		{
			if (objectList.isEmpty())
			{
				statement = statementProxy.getConnection().createStatement();
				resultSet = statement.executeQuery(sql);
			} else
			{
				PreparedStatement preparedStatement = statementProxy.getConnection().prepareStatement(sql);
				statement = preparedStatement;// 这样代码好看
				for (int i = 0; i < objectList.size(); i++)
				{
					preparedStatement.setObject(i + 1, objectList.get(i));
				}
				resultSet = preparedStatement.executeQuery();
			}
			beforeImage = Image.buildImage(tableMetaData, resultSet);

		} finally
		{
			if (resultSet != null)
			{
				resultSet.close();
			}
			if (statement != null)
			{
				statement.close();
			}
		}
		return beforeImage;
	}

	@Override
	protected Image afterImage(Image beforeImage) throws SQLException
	{
		return Image.empty(getTableMetaData());
	}
}
